# Define here the models for your scraped items
#
# See documentation in:
# https://docs.scrapy.org/en/latest/topics/items.html
import hashlib

import scrapy

from components.custom_item import UpdateCustomItem
from utils.tools import get_md5, format_date


class NetTaxHonorResponseItem(UpdateCustomItem):
    province = scrapy.Field()  # 省份
    city = scrapy.Field()  # 城市
    county = scrapy.Field()  # 区县
    document_title = scrapy.Field()  # 标题
    hierarchy = scrapy.Field()  # 标题
    publish_date = scrapy.Field()  # 发布时间
    decare_department = scrapy.Field()  # 发布部门
    document_url = scrapy.Field()  # 政策链接
    source = scrapy.Field()  # 来源
    item_count = scrapy.Field()  # 数量
    response = scrapy.Field()  # 响应
    deadline = scrapy.Field()  # 执行期限
    attachment_url = scrapy.Field()  # 附件链接
    uk_id = scrapy.Field()  # uk id md5(标题+发布时间+来源+来源地址)，先对标题和发布时间清洗，唯一标识
    document_id = scrapy.Field()  # 政策id md5(标题+发布时间)，先对标题和发布时间清洗，唯一标识)

    def pre_to_db(self):
        self.document_title = (self.document_title or "").strip()
        self.publish_date = format_date((self.publish_date or "").strip(), new_format="%Y-%m-%d")
        if not self.uk_id:
            self.uk_id = get_md5(self.document_title, self.publish_date, self.source, self.document_url)
            self.document_id = get_md5(self.document_title, self.publish_date)


class NetTaxHonorItem(UpdateCustomItem):
    __table_name__ = "net_tax_honor_item"
    document_id = scrapy.Field()  # 政策id md5(标题+发布时间)，先对标题和发布时间清洗，唯一标识
    province = scrapy.Field()  # 省份
    hierarchy = scrapy.Field()  # 级别
    city = scrapy.Field()  # 城市
    county = scrapy.Field()  # 区县
    document_title = scrapy.Field()  # 标题
    publish_date = scrapy.Field()  # 发布时间
    decare_department = scrapy.Field()  # 发布部门
    document_url = scrapy.Field()  # 政策链接
    project_type = scrapy.Field()  # 项目类型
    project_name = scrapy.Field()  # 项目名称
    company_name = scrapy.Field()  # 公司名称
    amount = scrapy.Field()  # 金额
    source = scrapy.Field()  # 来源
    deadline = scrapy.Field()  # 执行期限
    attachment_url = scrapy.Field()  # 附件链接
    uk_id = scrapy.Field()  # uk id md5(标题+发布时间+来源+来源地址)，先对标题和发布时间清洗，唯一标识

    def pre_to_db(self):
        """
        入库前的处理
        """
        self.document_title = (self.document_title or "").strip()
        self.publish_date = format_date((self.publish_date or "").strip(), new_format="%Y-%m-%d")
        if not self.uk_id:
            self.uk_id = get_md5(self.document_title, self.publish_date, self.source, self.document_url)
            self.document_id = get_md5(self.document_title, self.publish_date)
